#include "moverselection.h"
#include "GraphicsArea/drawingarea.h"
#include <iostream>
using namespace std;
//************************************************************* Constructor

MoverSelection::MoverSelection(DrawingArea* drawingAreaUse,QDockWidget* dockInfoToolP,int XBeginSelection,int XEndSelection,int YBeginSelection,int YEndSelection) : ToolEffect(drawingAreaUse){

    drawingAreaUseMoverSelection = drawingAreaUse;
    dockInfoTool = dockInfoToolP;
    imageMoverSelection = NULL;
    dejaDeplace = false;

    XBeginVraiSelection = max((int)drawingAreaUse->getCurrentLayer()->x(),XBeginSelection);
    YBeginVraiSelection = max((int)drawingAreaUse->getCurrentLayer()->y(),YBeginSelection);
    largeurVraiSelection = min((int)drawingAreaUse->getCurrentLayer()->x()+(int)drawingAreaUse->getCurrentLayer()->getWidth(),XEndSelection) - XBeginVraiSelection;
    hauteurVraiSelection = min((int)drawingAreaUse->getCurrentLayer()->y()+(int)drawingAreaUse->getCurrentLayer()->getHeight(),YEndSelection) - YBeginVraiSelection;

    drawingAreaUseMoverSelection->XBeginSelection = 0;
    drawingAreaUseMoverSelection->XEndSelection = 0;
    drawingAreaUseMoverSelection->YBeginSelection = 0;
    drawingAreaUseMoverSelection->YEndSelection=0;

    XDeplace = XBeginVraiSelection;
    YDeplace = YBeginVraiSelection;

    if (largeurVraiSelection >0){
        isMover = true;
    }else{
         isMover = false;
    }
}

//************************************************************* Affichage of Dock Tool Info

void MoverSelection::createAffichage(){
    QWidget* widgetInfoToolUp = new QWidget();
    QWidget* widgetInfoTool = new QWidget(widgetInfoToolUp);
    QGridLayout* layoutInfoTool = new QGridLayout();

    QLabel *labelInfoGenTool = new QLabel();

    labelInfoGenTool->setText(QString::fromUtf8("Déplacement de la sélection : cliquez sur l'image et déplacez-la"));

    layoutInfoTool->addWidget(labelInfoGenTool, 0, 0, Qt::AlignVCenter);

    widgetInfoTool->setLayout(layoutInfoTool);
    dockInfoTool->setWidget(widgetInfoToolUp);
}

//************************************************************* MoverSelection movement

void MoverSelection::initMoveSelection(int x, int y){
    if( isMover == true){

        if(dejaDeplace){
            distanceX = x - XDeplace;
            distanceY = y - YDeplace;
            movingSelection(x,y);
            return;
        }

        dejaDeplace = true;

        XBeginVraiSelectionDansLayer = XBeginVraiSelection -drawingAreaUseMoverSelection->getCurrentLayer()->x();
        YBeginVraiSelectionDansLayer = YBeginVraiSelection -drawingAreaUseMoverSelection->getCurrentLayer()->y();

        imageMoverSelection = new QImage(largeurVraiSelection,hauteurVraiSelection,QImage::Format_ARGB32);
        int u = 0;
        int v = 0;

        for(int i = XBeginVraiSelectionDansLayer; i<XBeginVraiSelectionDansLayer+largeurVraiSelection; i++){

            for(int j= YBeginVraiSelectionDansLayer; j< YBeginVraiSelectionDansLayer+hauteurVraiSelection; j++){

                imageMoverSelection->setPixel(u,v,drawingAreaUseMoverSelection->getCurrentLayer()->getImage()->pixel(i,j));

                drawingAreaUseMoverSelection->getCurrentLayer()->getImage()->setPixel(i, j,qRgba(255, 255, 255, 0));
                v++;
            }
            u++;
            v=0;
        }

        distanceX = x - XBeginVraiSelection;
        distanceY = y - YBeginVraiSelection;

        XDeplace = XBeginVraiSelection;
        YDeplace = YBeginVraiSelection;
    }
}

void MoverSelection::movingSelection(int x, int y){
    if( isMover == true){
        XDeplace = x - distanceX;
        YDeplace = y - distanceY;
    }
}

void MoverSelection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    if( isMover == true){
        if(imageMoverSelection != NULL){
            painter->drawImage( QRect(XDeplace,YDeplace,largeurVraiSelection, hauteurVraiSelection), *imageMoverSelection);
        }

    }
    QPen penWrite(Qt::white);

    QVector<qreal> dashesWrite;
    dashesWrite << 4 << 10;
    penWrite.setDashPattern(dashesWrite);
    penWrite.setDashOffset (0);
    painter->setPen(penWrite);

    painter->drawRect(XDeplace, YDeplace, largeurVraiSelection, hauteurVraiSelection);

    QPen pen(Qt::darkCyan);
    pen.setStyle(Qt::DashLine);
    pen.setDashOffset (2);
    QVector<qreal> dashes;

    dashes << 2 << 12;
    pen.setDashPattern(dashes);
    painter->setPen(pen);

    painter->drawRect(XDeplace, YDeplace, largeurVraiSelection, hauteurVraiSelection);
}
void MoverSelection::endMoverSelection(){
    if( isMover == true && dejaDeplace == true){
        FusionLayers* fusion = new FusionLayers(drawingAreaUseMoverSelection->getWidth(),drawingAreaUseMoverSelection->getHeight());
        fusion->fusionnerFitToLayer(imageMoverSelection,drawingAreaUseMoverSelection->getCurrentLayer(),XDeplace, YDeplace);
    }
}
